package net.zjitc.service.impl;

import net.zjitc.dao.UserDao;
import net.zjitc.dao.impl.UserDaoImpl;
import net.zjitc.pojo.User;
import net.zjitc.service.UserService;
import net.zjitc.utils.MailUtils;

import javax.mail.MessagingException;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2017/11/22.
 */
public class UserServiceImpl implements UserService{
    UserDao userDao = new UserDaoImpl();

    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public int findUsername(String username) {
        return userDao.findUsername(username);
    }

    @Override
    public int save(User user) {
        //没有状态码，设置状态 1表示激活 0表示未激活
        user.setState(0);
        String code= UUID.randomUUID().toString()+UUID.randomUUID();
        user.setCode(code);
        //发送一个邮件
        try {
            MailUtils.sendMail(user.getEmail(),code);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return userDao.save(user);
    }

    @Override
    public User getState(String username, String password) {
        return userDao.getState(username, password);
    }

    @Override
    public User findByCode(String code) {
        return userDao.findByCode(code);
    }

    @Override
    public int update(User user) {
        return userDao.update(user);
    }


}
